<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>弹球</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .ball {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background: radial-gradient(at 30px 25px, #fff, red);
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>
</head>

<body>
    <div class="ball"></div>
    <script>
        let ball = document.querySelector(".ball"); // 球元素
        let maxX = window.innerWidth - 100;  // 最大可移动距离

        let x = 0, moveX = 5;

        setInterval(function () {
            x += moveX;
            if (x > maxX || x < 0) {
                moveX = -moveX;
            }
            // if (x > maxX) {
            //     moveX = -5;
            // }
            // if (x < 0) {
            //     moveX = 5;
            // }
            ball.style.cssText = `
                position: absolute;
                left: ${x}px;
            `;

            // if (x < maxX) {
            //     ball.style.cssText = `
            //         position: absolute;
            //         left: ${x++}px;
            // `;
            // } else {
            //     ball.style.cssText = `
            //         position: absolute;
            //         left: ${x--}px;
            // `;
            // }

        }, 10);
    </script>
</body>

</html>